package com.ruoyi.fun.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.fun.mapper.EcLogisticsMapper;
import com.ruoyi.fun.domain.EcLogistics;
import com.ruoyi.fun.domain.EcOrder;
import com.ruoyi.fun.service.IEcLogisticsService;
import com.ruoyi.fun.service.IEcOrderService;
import com.ruoyi.common.utils.StringUtils;

/**
 * 物流信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-08-06
 */
@Service
public class EcLogisticsServiceImpl implements IEcLogisticsService 
{
    @Autowired
    private EcLogisticsMapper ecLogisticsMapper;
    
    @Autowired
    private IEcOrderService ecOrderService;

    /**
     * 查询物流信息
     * 
     * @param logisticsId 物流信息主键
     * @return 物流信息
     */
    @Override
    public EcLogistics selectEcLogisticsByLogisticsId(Long logisticsId)
    {
        return ecLogisticsMapper.selectEcLogisticsByLogisticsId(logisticsId);
    }

    /**
     * 查询物流信息列表
     * 
     * @param ecLogistics 物流信息
     * @return 物流信息
     */
    @Override
    public List<EcLogistics> selectEcLogisticsList(EcLogistics ecLogistics)
    {
        return ecLogisticsMapper.selectEcLogisticsList(ecLogistics);
    }

    /**
     * 新增物流信息
     * 
     * @param ecLogistics 物流信息
     * @return 结果
     */
    @Override
    public int insertEcLogistics(EcLogistics ecLogistics)
    {
        ecLogistics.setCreateTime(DateUtils.getNowDate());
        int result = ecLogisticsMapper.insertEcLogistics(ecLogistics);
        
        // 注释掉自动更新订单状态的逻辑，状态变更统一通过微信回调接口处理
        // if (result > 0 && StringUtils.isNotEmpty(ecLogistics.getOrderNo())) {
        //     updateOrderStatusIfNeeded(ecLogistics.getOrderNo(), ecLogistics.getOrgId());
        // }
        
        System.out.println("物流信息新增成功，订单号: " + ecLogistics.getOrderNo() + "，状态变更将通过微信回调接口统一处理");
        
        return result;
    }
    
    /**
     * 根据订单号查询订单，如果订单存在且状态为2，则更新为状态3并设置发货时间
     * 注释掉此方法，因为订单状态变更统一通过微信回调接口处理，避免状态混乱
     * 
     * @param orderNo 订单号
     * @param orgId 组织ID
     */
    /*
    private void updateOrderStatusIfNeeded(String orderNo, Long orgId) {
        try {
            // 根据订单号查询订单
            EcOrder queryOrder = new EcOrder();
            queryOrder.setOrderNo(orderNo);
            queryOrder.setOrgId(orgId);
            List<EcOrder> orderList = ecOrderService.selectEcOrderList(queryOrder);
            
            if (orderList != null && !orderList.isEmpty()) {
                EcOrder order = orderList.get(0);
                // 如果订单状态为2（待发货），则更新为状态3（待收货）并设置发货时间
                if ("2".equals(order.getOrderStatus())) {
                    order.setOrderStatus("3");
                    order.setDeliveryTime(DateUtils.getNowDate()); // 设置发货时间为当前时间
                    ecOrderService.updateEcOrder(order);
                    System.out.println("订单状态已自动更新：订单号=" + orderNo + "，状态从2更新为3，发货时间=" + DateUtils.getNowDate());
                }
            }
        } catch (Exception e) {
            // 记录异常但不影响物流信息的新增
            System.err.println("更新订单状态时发生异常：" + e.getMessage());
            e.printStackTrace();
        }
    }
    */

    /**
     * 修改物流信息
     * 
     * @param ecLogistics 物流信息
     * @return 结果
     */
    @Override
    public int updateEcLogistics(EcLogistics ecLogistics)
    {
        ecLogistics.setUpdateTime(DateUtils.getNowDate());
        return ecLogisticsMapper.updateEcLogistics(ecLogistics);
    }

    /**
     * 批量删除物流信息
     * 
     * @param logisticsIds 需要删除的物流信息主键
     * @return 结果
     */
    @Override
    public int deleteEcLogisticsByLogisticsIds(Long[] logisticsIds)
    {
        return ecLogisticsMapper.deleteEcLogisticsByLogisticsIds(logisticsIds);
    }

    /**
     * 删除物流信息信息
     * 
     * @param logisticsId 物流信息主键
     * @return 结果
     */
    @Override
    public int deleteEcLogisticsByLogisticsId(Long logisticsId)
    {
        return ecLogisticsMapper.deleteEcLogisticsByLogisticsId(logisticsId);
    }

    /**
     * 根据logisticsId和组织ID查询Logistics
     * 
     * @param logisticsId Logistics主键
     * @param orgId 组织ID
     * @return Logistics
     */
    @Override
    public EcLogistics selectEcLogisticsByLogisticsIdAndOrgId(Long logisticsId, Long orgId)
    {
        return ecLogisticsMapper.selectEcLogisticsByLogisticsIdAndOrgId(logisticsId, orgId);
    }

    /**
     * 根据组织ID查询Logistics列表
     * 
     * @param eclogistics Logistics
     * @param orgId 组织ID
     * @return Logistics集合
     */
    @Override
    public List<EcLogistics> selectEcLogisticsListByOrgId(EcLogistics eclogistics, Long orgId)
    {
        return ecLogisticsMapper.selectEcLogisticsListByOrgId(eclogistics, orgId);
    }
    
    /**
     * 根据订单号查询物流信息
     * 
     * @param orderNo 订单号
     * @return 物流信息
     */
    @Override
    public EcLogistics selectEcLogisticsByOrderNo(String orderNo) {
        return ecLogisticsMapper.selectEcLogisticsByOrderNo(orderNo);
    }
}